/*
 * Copyright (C) 2017 Gautam Chibde
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.chibde.audiovisualizer.sample.visualizer;

import com.chibde.audiovisualizer.sample.BaseAbility;
import com.chibde.visualizer.BarVisualizer;
import com.chibde.visualizer.ResourceTable;
import ohos.agp.components.Component;
import ohos.agp.components.Image;
import ohos.agp.components.Text;
import ohos.agp.utils.Color;
import ohos.media.audio.AudioRenderer;

public class BarVisualizerAbility extends BaseAbility {
    private boolean isfirst = true;
    private long currentClick = 0L;
    private Image im_pause;

    @Override
    public void init() {
        BarVisualizer barVisualizer = (BarVisualizer) findComponentById(ResourceTable.Id_visualizer);

        // set custom color to the line.
        if (barVisualizer != null) {
            barVisualizer.setColor(Color.BLUE.getValue());
            barVisualizer.setDensity(40);
            barVisualizer.setPlayer(playSoundUtil.getAudioRenderer().getRendererSessionId(), getBundleName());
        }

        // define custom number of bars you want in the visualizer between (10 - 256).
        // Set your media player to the visualizer.
        Image im_start = (Image) findComponentById(ResourceTable.Id_im_start);
        Image im_reset = (Image) findComponentById(ResourceTable.Id_im_reset);
        im_pause = (Image) findComponentById(ResourceTable.Id_im_pause);
        Text title_name = (Text) findComponentById(ResourceTable.Id_tv_title_name);
        title_name.setText("Bar Visualizer");
        Component back = findComponentById(ResourceTable.Id_im_back);
        back.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                terminateAbility();
            }
        });
        im_pause.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        playSoundUtil.getAudioRenderer().pause();
                    }
                }).start();
                isfirst = true;
                im_start.setVisibility(Component.VISIBLE);
                im_pause.setVisibility(Component.HIDE);
            }
        });
        im_start.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                if (currentClick != 0 && (System.currentTimeMillis() - currentClick <= 1000)) {
                    return;
                }
                currentClick = System.currentTimeMillis();
                Thread thread = new Thread(new Runnable() {
                    @Override
                    public void run() {
                        playSoundUtil.getAudioRenderer().setPosition(playSoundUtil.getAudioRenderer().getPosition());
                        playSoundUtil.getAudioRenderer().start();
                        playSoundUtil.loadSound("Ring10.wav");
                    }
                });
                thread.start();
                isfirst = false;
                im_start.setVisibility(Component.HIDE);
                im_pause.setVisibility(Component.VISIBLE);
            }
        });
        im_reset.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                if (!isfirst) {
                    if (playSoundUtil.getAudioRenderer().getState() != AudioRenderer.State.STATE_PLAYING) {
                        return;
                    }
                    new Thread(new Runnable() {
                        @Override
                        public void run() {
                            playSoundUtil.getAudioRenderer().pause();
                            try {
                                Thread.sleep(100);
                                playSoundUtil.getAudioRenderer().start();
                                playSoundUtil.loadSound("Ring10.wav");
                            } catch (InterruptedException e) {
                                e.fillInStackTrace();
                            }
                        }
                    }).start();
                }
            }
        }
        );
    }

    @Override
    protected int getLayout() {
        return ResourceTable.Layout_ability_bar_visualizer;
    }
}
